# 导入数据库访问类
import db


class StuInfo:
    # 定义属性
    id = 0
    name = ""
    sex = "1"
    cdate = "2018-05-05"
    birthday = "2018-12-11"
    stuid = "0"

    # 定义方法
    # 增加元组数据 data结构为（name,sex,cdate,birthday,stuid）
    def addrecord(self, data):
        print(self.name)
        sql="insert into stuinfo(name,sex,cdate,birthday,stuid) values('%s','%s','%d','%d','%s')"%(data)
        try:
            mydba = db.MyDB
            num = 0
            num = mydba.executesql(sql)
            print("新增{0}条",num)
        except Exception as err:
            print(err)

    # 删除指定记录号的记录
    def delrecord(self, rowid):
        sql="delete from stuinfo where id=%d"%(rowid)
        print(sql)
        try:
            mydba = db.MyDB
            num = 0
            num = mydba.executesql(mydba,sql)
            print("删除%d条"%(num))

        except Exception as err:
            print(err)

    def findall(self):
        sql = "select * from stuinfo"
        try:
            mydba = db.MyDB
            data = mydba.querysql(mydba, sql)
            for d1 in data:
                print(d1)
        except Exception as err:
            print("执行错误：",err)

    # 更新数据data（name,sex,cdate,birthday,stuid）
    def updaterecord(self,data):
         sql="update stuinfo set name='%s',sex='%s',cdate='%s',birthday='%s',stuid='%s' where id='%d'"%(data)
         try:
             mydba = db.MyDB
             num = 0
             num = mydba.executesql(mydba,sql)
         except Exception as err:
             print("执行错误：", err)